package com.icinfo.cloud.provider.common.crontask;

import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.core.config.RedisRepository;
import com.icinfo.cloud.provider.punish.dw.service.IDwCaseOrgDailyService;
import com.icinfo.cloud.provider.punish.evideinv.service.EvidenceInventoryService;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 证据库案件结果定时
 * @author Administrator
 */
@Component
@Slf4j
public class EvidenceCaseResultSyncTask {

    @Resource
    private CaseMainInfoMapper caseMainInfoMapper;

    @Resource
    private EvidenceInventoryService evidenceInventoryService;

    @Resource
    private RedisRepository redisRepository;

    @Resource
    private IDwCaseOrgDailyService dwCaseOrgDailyService;

    private final String failKey = "SYNC_EVIDENCE_CASE_RESULT_FAIL_LIST";

    /**
     * 案件结果证据库同步，每日04时执行
     */
    // @Scheduled(cron = "0 0 4 * * ?")
    public void syncResult() {
        // 查询前一天已结案数据
        Example example = new Example(CaseMainInfo.class);
        example.createCriteria().andLike("caseCurrentStageName","结案").andBetween("caseEndTime", LocalDateTime.now().minusDays(1).toLocalDate().atStartOfDay(),LocalDateTime.now());
        List<CaseMainInfo> list = caseMainInfoMapper.selectByExample(example);
        if (!list.isEmpty()){
            List<String> failList = new ArrayList<>();
            for (CaseMainInfo caseMainInfo : list) {
                try {
                    evidenceInventoryService.saveCaseResultInfo(caseMainInfo.getId());
                } catch (Exception e) {
                    failList.add(caseMainInfo.getId());
                    log.warn("案件结果证据库同步异常caseId：caseMainInfo.getId()",e);
                }
            }
            // 处理失败队列
            String fl = redisRepository.get(failKey);
            if (StringUtils.isNotBlank(fl)){
                List<String> fails = JSONObject.parseArray(fl,String.class);
                for (int i = 0; i <fails.size(); i++) {
                    try {
                        evidenceInventoryService.saveCaseResultInfo(fails.get(i));
                    } catch (Exception e) {
                        failList.add(fails.get(i));
                        log.warn("案件结果证据库同步异常caseId：caseMainInfo.getId()",e);
                    }
                }
                if (fails.size() > 500){
                    log.warn("案件结果证据库同步失败队列超过500，请手动处理,数据为:{}",fl);
                }
            }

            if (!failList.isEmpty()){
                redisRepository.set(failKey, JSONObject.toJSONString(failList));
            }
        }
    }

    /**
     * 同步鉴定告知书
     */
    // @Scheduled(cron = "0 0 3 * * ?")
    public void syncAppraisal() {
        evidenceInventoryService.saveAppraisalDoc();
    }


    /**
     * 定时统计案件信息
     */
//    @Scheduled(cron = "0 0 3 * * ?")
    public void caseStatic() {
        dwCaseOrgDailyService.statisticsCaseInfo();
    }
}
